package com.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MyFrame extends JFrame {

	private MyPanel my = null;

	public MyFrame() {
		// 初始化

		this.setSize(800, 600);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new FlowLayout());

		my = new MyPanel();
		Dimension ds = new Dimension(800, 560);

		my.setPreferredSize(ds);
		this.addKeyListener(new YiDong());
		this.add(my);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		my.run();
	}

	class YiDong implements KeyListener {

		@Override
		public void keyPressed(KeyEvent keys) {
			if (keys.getKeyCode() == KeyEvent.VK_UP) {
				// 上
				my.getShapes().updateDirection();
				my.repaint();
			} else if (keys.getKeyCode() == KeyEvent.VK_DOWN) {
				//下
				my.getShapes().toXia();
				my.repaint();
			} else if (keys.getKeyCode() == KeyEvent.VK_LEFT) {
				//左
				my.getShapes().toZuo();
				my.repaint();
			} else if (keys.getKeyCode() == KeyEvent.VK_RIGHT) {
				//右
				my.getShapes().toYou();
				my.repaint();
			}
		}

		@Override
		public void keyReleased(KeyEvent arg0) {

		}

		@Override
		public void keyTyped(KeyEvent arg0) {

		}

	}

}
